/*
 * DigitalClock.java
 *
 * Created on February 27, 2003, 8:08 PM
 */

package com.openbravo.beans;

/**
 *
 * @author  rlittle
 */
public class DigitalClock extends javax.swing.JTextField implements java.io.Serializable, java.lang.Runnable {
    
    /** Creates new form DigitalClock */
    public DigitalClock() {
        this.start();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents

        setEditable(false);
        setHorizontalAlignment(javax.swing.JTextField.CENTER);
        setText("digitalClock");
    }//GEN-END:initComponents
    
    public void run() {
        while(my_Thread != null) {
            update();
            try {
                // Note, this will have a building delay since it will not
                // sleep until the update method is complete.  So if it takes
                // 10 ms for update to complete, the next update will not
                // occur until updateInterval+10
                // Need to find a way to resolve this.
                Thread.sleep(updateInterval);
            }
            catch (InterruptedException e) {
/**                log.logDebug(getClass().getName(), "run", e.toString()); */
            }
        }
    }
    
    protected void update() {
        currentTime = new java.util.Date();
        this.display = displayFormatter.format(currentTime);
        this.tooltip = toolTipFormatter.format(currentTime);
        
        if (!this.display.equals(this.getText())) {
            this.setText(this.display);
        }
        if (!this.tooltip.equals(this.getToolTipText())) {
            this.setToolTipText(this.tooltip);
        }
        
    }
    
    /** Getter for property displayFormatter.
     * @return Value of property displayFormatter.
     */
    public java.text.SimpleDateFormat getDisplayFormatter() {
        return this.displayFormatter;
    }
    
    /** Setter for property displayFormatter.
     * @param displayFormatter New value of property displayFormatter.
     */
    public void setDisplayFormatter(java.text.SimpleDateFormat displayFormatter) {
        this.displayFormatter = displayFormatter;
    }
    
    /** Getter for property toolTipFormatter.
     * @return Value of property toolTipFormatter.
     */
    public java.text.SimpleDateFormat getToolTipFormatter() {
        return this.toolTipFormatter;
    }
    
    /** Setter for property toolTipFormatter.
     * @param toolTipFormatter New value of property toolTipFormatter.
     */
    public void setToolTipFormatter(java.text.SimpleDateFormat toolTipFormatter) {
        this.toolTipFormatter = toolTipFormatter;
    }
    
    /** Getter for property font.
     * @return Value of property font.
     */
    @Override
    public java.awt.Font getFont() {
        return this.font;
    }
    
    /** Setter for property font.
     * @param font New value of property font.
     */
    @Override
    public void setFont(java.awt.Font font) {
        this.font = font;
    }
    
    /** Getter for property updateInterval
     * @return Value of updateInterval
     */
    public int getUpdateInterval() {
        return this.updateInterval;
    }
    
    /** Setter for property font.
     * @param font New value of property font.
     */
    public void setUpdateInterval(int updateInterval) {
        this.updateInterval = updateInterval;
        my_Thread.interrupt();  // Interrupt the thread so new delay will take effect now.
    }
    
    public void start() {
        my_Thread = new java.lang.Thread(this);
        my_Thread.start();
    }
    
    public void stop() {
        my_Thread = null;
    }
    
    /** Getter for property displayFormat.
     * @return Value of property displayFormat.
     */
    public String getDisplayFormat() {
        return this.displayFormat;
    }
    
    /** Setter for property displayFormat.
     * @param displayFormat New value of property displayFormat.
     */
    public void setDisplayFormat(String displayFormat) {
        this.displayFormat = displayFormat;
        this.setDisplayFormatter(new java.text.SimpleDateFormat(displayFormat));
    }
    
    /** Getter for property tooltipFormat.
     * @return Value of property tooltipFormat.
     */
    public String getTooltipFormat() {
        return this.tooltipFormat;
    }
    
    /** Setter for property tooltipFormat.
     * @param tooltipFormat New value of property tooltipFormat.
     */
    public void setTooltipFormat(String tooltipFormat) {
        this.tooltipFormat = tooltipFormat;
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
    
    private Thread my_Thread;
    
    private java.util.Date currentTime = new java.util.Date();
    
    private int updateInterval = 60000;
    
    /** Holds value of property displayFormat. */
    private java.text.SimpleDateFormat displayFormatter = new java.text.SimpleDateFormat("h:mm a");
    
    /** Holds value of property toolTipFormat. */
    private java.text.SimpleDateFormat toolTipFormatter = new java.text.SimpleDateFormat("EEEE, MMMM d, yyyy");
    
    /** Holds value of property font. */
    private java.awt.Font font;
    
    /** private com.rl.debug.Debug log = com.rl.debug.Debug.getLogger(); */
    
    /** Holds value of property displayFormat. */
    private String displayFormat;
    
    /** Holds value of property tooltipFormat. */
    private String tooltipFormat;
    
    private String display;
    
    private String tooltip;
    
}
